package com.hisicom.scqxspxcjc.sceneCheck.resultDecision.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hisicom.scqxspxcjc.base.dao.BaseRepository;
import com.hisicom.scqxspxcjc.base.service.BaseService;
import com.hisicom.scqxspxcjc.sceneCheck.resultDecision.domain.ResultDecisionBusiness;
import com.hisicom.scqxspxcjc.sceneCheck.resultDecision.exception.ResultDecisionBusinessException;
import com.hisicom.scqxspxcjc.sceneCheck.resultDecision.repostion.ResultDecisionBusinessRepository;
import lombok.NonNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhonggls
 * @date 2020/12/21 14:12
 * @Explain
 */
@Service
public class ResultDecisionBusinessService implements BaseService<ResultDecisionBusiness, String> {
    @Autowired
    private ResultDecisionBusinessRepository resultDecisionBusinessRepository;

    @Override
    public BaseRepository<ResultDecisionBusiness, String> getRepository() {
        return resultDecisionBusinessRepository;
    }

    public ResultDecisionBusiness saveInfo(ResultDecisionBusiness resultDecisionBusiness) {
        if (StrUtil.isBlank(resultDecisionBusiness.getChapterNumber())) {
            throw new ResultDecisionBusinessException("章节序号不能为空!");
        }
        if (StrUtil.isBlank(resultDecisionBusiness.getIfKey())) {
            throw new ResultDecisionBusinessException("是否关键项不能为空!");
        }
        if (StrUtil.isBlank(resultDecisionBusiness.getGroupId())) {
            throw new ResultDecisionBusinessException("分组id不能为空!");
        }
        if (StrUtil.isNotBlank(resultDecisionBusiness.getId())) {
            ResultDecisionBusiness resultDecisionBusiness1 = findById(resultDecisionBusiness.getId());
            if (ObjectUtil.isNotEmpty(resultDecisionBusiness1)) {
                BeanUtils.copyProperties(resultDecisionBusiness, resultDecisionBusiness1);
                return BaseService.super.save(resultDecisionBusiness1);
            }
        }
        return BaseService.super.save(resultDecisionBusiness);
    }

    public List<ResultDecisionBusiness> findByGroupIdOrderBySort(@NonNull String groupId) {
        return resultDecisionBusinessRepository.findByGroupIdOrderBySort(groupId);
    }

    public List<ResultDecisionBusiness> findByGroupIdAndChapterNumber(@NonNull String groupId, @NonNull String chapterNumber) {
        return resultDecisionBusinessRepository.findByGroupIdAndChapterNumber(groupId, chapterNumber);
    }
    public List<ResultDecisionBusiness> findByGroupIdAndResultNo(@NonNull String groupId, @NonNull String resultNo) {
        return resultDecisionBusinessRepository.findByGroupIdAndResultNo(groupId, resultNo);
    }


    /**
     * 保存"缺陷和问题描述" 对象集合
     *
     * @param params "缺陷和问题描述"对象集合数据
     * @return 成功/失败
     */
    public void addDecisionBusiness(Map params) {
        String owningDefect = (String) params.get("owningDefect");
        String groupId = (String) params.get("groupId");
        if (StrUtil.isBlank(groupId)) {
            throw new ResultDecisionBusinessException("保存\"缺陷和问题描述\" 时分组id为空，请联系管理员！");
        }
        String mainBusinessId = (String) params.get("mainBusiness");
        List<ResultDecisionBusiness> list = JSONArray.parseArray(JSON.toJSONString(params.get("data")), ResultDecisionBusiness.class);
        //判断对象是否存在，存在则修改，不存在则新增。
        if (ObjectUtil.isNotEmpty(list)) {
            for (ResultDecisionBusiness resultDecisionBusiness : list) {
                if (StrUtil.isBlank(resultDecisionBusiness.getId())) {
                    if (StrUtil.isBlank(resultDecisionBusiness.getChapterNumber())) {
                        throw new ResultDecisionBusinessException("章节序号不能为空，请联系管理员");
                    }
                    List<ResultDecisionBusiness> list1 = findByGroupIdAndChapterNumber(groupId, resultDecisionBusiness.getChapterNumber());
                    if (list1.size() > 0) {
                        delete(list1);
                    }
                }
                resultDecisionBusiness.setOwningDefect(owningDefect);
                resultDecisionBusiness.setMainBusiness(mainBusinessId);
                resultDecisionBusiness.setGroupId(groupId);
                saveInfo(resultDecisionBusiness);
            }
        }
    }
}
